var DayList = require('../../utils/util.js'); 
var appData = getApp().globalData;
Page({
    data:{
        loading:false,
        linked:false,
        scrollleft:0,
        scrollCurrent:0,
        randtype:'',
        act1:'act',//日
        act2:'',//月
        act3:'',//年
        users:[],
        date:'',
        daynum:10,
        weeknum:5,
        monthnum:5,
        days:[],
        weeks:[],
        months:[],
        currentDay:'',//
        currentWeek:'',
        currentMonth:'',
        max_heart:getApp().globalData.UserData.max_heart,
        moredayTime:0,//上次刷新时间
        datalist:[]
    },
    onLoad(){
        let now =new Date();
        let nowStr = this.dateAny(now);
        this.setData({
            date:nowStr

        })
       this.showdata(nowStr)
    },
    showdata:function(date){
        let self = this;
        if (self.mydata.requsting)return;
        self.mydata.requsting = true;
        self.setData({ loading: true })
        getApp().request({
            url:'heart_record_list',
            data:{
                date:date
            },success(res){
                let info = res.data;
                let tmplist=[];
                if(info.flag){
                    let dt =info.data;
                    for(let i=0;i<dt.length;++i){                
                    let max = dt[i].max_heart;
                    let avg = dt[i].avg_heart;
                    let oldmax = self.data.max_heart;
                    let maxper = Math.floor(max*100/oldmax)
                    let avgper = Math.floor(avg*100/oldmax)
                    tmplist.push({max:max+"/"+maxper,avg:avg+"/"+avgper,cost:dt[i].cost_time,calorie:dt[i].calorie,date:dt[i].create_time.substr(0,10),time:dt[i].create_time.substr(11,5) })
                    }
                    self.setData({datalist:tmplist});
                }else{
                }

                self.setData({ loading: false })
            },fail(){
                wx.showModal({
                    content: '请求失败',
                    showCancel:false,
                    success: function(res) {
                    }
                })
            },complete(){
                self.setData({loading:false})
                setTimeout(function(){
                  self.mydata.requsting = false;
                },300)
                console.error("requsting over");
            }
        })
    },
    onReady:function(e){
        var now  =Date.now();
        var self  =this;
        self.setData({
            days:DayList.beforedays(now)
        })
        setTimeout(function(){
            self.setData({
                scrollleft:self.data.scrollleft+400
            })
        })
    },
    linkHeart:function(){
        this.setData({
            linked:true
        })
    },
    start:function(){
        // wx.navigateTo
    },
    dateAny(date){
        let y = date.getFullYear();
        let m = date.getMonth();
        let d = date.getDate();
        let dateStr = y+"-"+(m>8?m:"0"+(m+1))+"-"+(d>9?d:"0"+d);
        return dateStr;
    },
    select:function(event){
        var Data = event.target.dataset;
        switch(Data.index){
            case "1":
                this.setData({act1:'act',act2:'',act3:''})
                break;
            case "2":
                this.setData({act1:'',act2:'act',act3:'',weeks:DayList.beforeweeks(Date.now()),scrollleft:420})
                break;
            case "3":
                this.setData({act1:'',act2:'',act3:'act',months:
                DayList.beforemonths(Date.now()),scrollleft:420})
                break;
        }
    }
    ,moreday:function(){
        // 拉取更多日详情
        var self = this;
        if(Date.now()-this.data.moredayTime>1000){
            this.setData({
                moredayTime: Date.now()
            })
            var days = this.data.days;
            var moredays = DayList.beforedays(days[0]['timestamp']);
            var len = moredays.length;
            for(var item in moredays){
                days.splice(item,0,moredays[item]) 
            }
            setTimeout(function(){
                self.setData({
                    daynum:self.data.daynum+10,
                    days:days,
                    scrollleft:self.data.scrollCurrent+400
                })
            },500)
        }
    },
    mydata:{
      requsting:false
    },
    selectdayby:function(index){
        let selectDate;
        console.debug(index);
        // if(!appData.isIOS)index = index+1;
        if(index>=this.data.days.length)selectDate =new Date();
        else selectDate = new Date(this.data.days[index].timestamp);
        this.showdata(this.dateAny(selectDate));
    },
    selectday:function(e){
        //点击具体- 日
      var index = e.target.dataset.index;
        this.setData({
            scrollleft:(index-1)*40,
            currentDay:index
        })
        this.selectdayby(index);
    },
    dayTouchEnd:function(e){ 
      var self = this;
      setTimeout(function () {
        var left = self.data.scrollCurrent;
        var offset = left / 40;
        var index = Math.round(offset);
        self.selectdayby(index);
        self.setData({
          scrollleft: (index) * 40-10,
          currentDay: index
        })
      }, 500)
        
    },
    moreweek:function(){
        // 拉取更多日详情
        var self = this;
        if(Date.now()-this.data.moredayTime>1000){
            this.setData({
                moredayTime: Date.now()
            })
            var weeks = this.data.weeks;
            var moredweeks = DayList.beforeweeks(weeks[0]['timestamp']);
            var len = moredweeks.length;
            for(var item in moredweeks){
                weeks.splice(item,0,moredweeks[item]) 
            }
            setTimeout(function(){
                self.setData({
                    weeknum:self.data.weeknum+5,
                    weeks:weeks,
                    scrollleft:self.data.scrollCurrent+400
                })
            },500)
        }
    },
    selectweek:function(e){
        //点击具体- 周
        var index = e.target.dataset.index;
        this.setData({
            scrollleft:(index)*80+20,
            currentWeek:index
        })
    },
    weekTouchEnd:function(e){ 
        var self = this;
        this.mydata.moving = false;
        // setTimeout(function(){
            var left = self.data.scrollCurrent;
            var offset = (left-20)/80;
            var index = Math.round(offset);
            self.setData({
                scrollleft:(index)*80+20,
                currentWeek:index
            })
        // },300)
    },
    moremonth:function(){
        // 拉取更多月详情
        var self = this;
        if(Date.now()-this.data.moredayTime>1000){
            this.setData({
                moredayTime: Date.now()
            })
            var weeks = this.data.weeks;
            var moredweeks = DayList.beforeweeks(weeks[0]['timestamp']);
            var len = moredweeks.length;
            for(var item in moredweeks){
                weeks.splice(item,0,moredweeks[item]) 
            }
            setTimeout(function(){
                self.setData({
                    weeknum:self.data.weeknum+5,
                    weeks:weeks,
                    scrollleft:self.data.scrollCurrent+400
                })
            },500)
        }
    },
    selectmonth:function(e){
        //点击具体- 周
        var index = e.target.dataset.index;
        this.setData({
            scrollleft:(index)*80+20,
            currentWeek:index
        })
    },
    monthTouchEnd:function(e){ 
        var self = this;
        // setTimeout(function(){
            var left = self.data.scrollCurrent;
            var offset = (left-20)/80;
            var index = Math.round(offset);
            self.setData({
                scrollleft:(index)*80+20,
                currentWeek:index
            })
        // },300)
    },
    scrollmove:function(e){
        this.setData({
            scrollCurrent:e.detail.scrollLeft
        })
    }, longtap(){
    }
})